package main

import "fmt"

func main() {
	//数组转成切片
	var arr1 [6]int
	//容量是从切片的起始位置到数组末尾的元素个数。
	var slice1 []int = arr1[2:5]
	for i := 0; i < len(arr1); i++ {
		arr1[i] = i
	}
	// print the slice
	for i := 0; i < len(slice1); i++ {
		fmt.Printf("Slice at %d is %d\n", i, slice1[i])
	}
	fmt.Printf("The length of arr1 is %d\n", len(arr1))
	fmt.Printf("The length of slice1 is %d\n", len(slice1))
	fmt.Printf("The capacity of slice1 is %d\n", cap(slice1))

	// grow the slice
	slice1 = slice1[0:4]
	for i := 0; i < len(slice1); i++ {
		fmt.Printf("Slice at %d is %d\n", i, slice1[i])
	}
	fmt.Printf("The length of slice1 is %d\n", len(slice1))
	fmt.Printf("The capacity of slice1 is %d\n", cap(slice1))

	//将切片传递给函数
	var arr2 = []int{1, 2, 3, 4, 5, 6, 7}
	fmt.Println("The sum of the array is", sum(arr2))

}

// 将切片传递给函数
func sum(s []int) (sum int) {
	for i := range s {
		sum += i
	}
	return
}
